package com.moneywelcome.investassembly.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.moneywelcome.exceptions.MwcArgsException;
import com.moneywelcome.goldfacade.GoldQueryApi;
import com.moneywelcome.goldfacade.dto.GoldInfoDto;
import com.moneywelcome.goldfacade.dto.SubscribeRuleDto;
import com.moneywelcome.http.ServletUtil;
import com.moneywelcome.investfacade.dto.SubscribeDto;
import com.moneywelcome.investfacade.dto.SubscribeRecordDto;
import com.moneywelcome.investfacade.vo.RecordPageVo;
import com.moneywelcome.investfacade.vo.SubscribeVo;
import com.moneywelcome.orderfacade.FinancialApi;
import com.moneywelcome.orderfacade.OrderCreatApi;
import com.moneywelcome.orderfacade.OrderQueryApi;
import com.moneywelcome.orderfacade.dto.SubscribeOrderDto;
import com.moneywelcome.orderfacade.dto.UserFinancialDto;
import com.moneywelcome.orderfacade.vo.InvestRecordVo;
import com.moneywelcome.orderfacade.vo.OrderPageVo;
import com.moneywelcome.results.PageResult;
import com.moneywelcome.results.Result;
import com.moneywelcome.results.StatusCode;
import com.moneywelcome.validate.ValidationUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import com.moneywelcome.investfacade.InvestSubscribeApi;

import javax.annotation.Resource;

@RestController
@Slf4j
@Api(tags = "用户投资购买服务")
public class InvestSubscribeApiImpl  implements InvestSubscribeApi {
    @Autowired
    private OrderCreatApi ocApi;
    @Autowired
    private OrderQueryApi oqApi;
    @Autowired
    private GoldQueryApi gqApi;
//    @Resource
//    private UserApi userApi;
    @Autowired
    private FinancialApi financialApi;

    @ApiOperation("黄金申购")
    @Override
    public Result<SubscribeDto> subscribe(SubscribeVo subscribeVo) {
        InvestRecordVo investRecordVo = BeanUtil.copyProperties(subscribeVo, InvestRecordVo.class);
        //用户信息校验
        //获取用户id
        Integer userId = Integer.valueOf(ServletUtil.getHttpServletRequest().getHeader("userId"));
//        UserInfoDto userInfoDto = userApi.selUserinfo(userId).getData();
        investRecordVo.setUserId(userId);
//        investRecordVo.setNickName(userInfoDto.getNickname());
        //黄金产品信息获得
        GoldInfoDto gold = gqApi.queryById(subscribeVo.getGoldId()).getData();
        if(BeanUtil.isEmpty(gold)) throw new MwcArgsException("黄金产品不存在", StatusCode.Error);
        investRecordVo.setVariety(gold.getVariety());
        //持仓信息获得
        UserFinancialDto financialDto = financialApi.queryByGoldId(subscribeVo.getGoldId(), userId).getData();
        investRecordVo.setFinancialId(financialDto.getFinancialId());
        //购买规则
        SubscribeRuleDto data = gqApi.querySubRule(gold.getGoldId()).getData();
        investRecordVo.setBuyRate(data.getBuyRate());
        investRecordVo.setManageRate(data.getManageRate()+ data.getServiceRate());
        SubscribeOrderDto orderDto = ocApi.subscribeOrder(investRecordVo).getData();
        return Result.success(BeanUtil.copyProperties(orderDto,SubscribeDto.class));
    }



    @ApiOperation("分页查询申购记录")
    @Override
    public Result<PageResult<SubscribeRecordDto>> subscribeRecord(RecordPageVo srp) {
        ValidationUtils.validate(srp);
        //获取用户id
        Integer userId = Integer.valueOf(ServletUtil.getHttpServletRequest().getHeader("userId"));
        OrderPageVo orderPageVo = BeanUtil.copyProperties(srp, OrderPageVo.class);
        orderPageVo.setUserId(userId);


        PageResult<SubscribeOrderDto> data = oqApi.subscribeRecord(orderPageVo).getData();
        PageResult<SubscribeRecordDto> pageResult = BeanUtil.copyProperties(data, PageResult.class);
        pageResult.setData(BeanUtil.copyToList(data.getData(),SubscribeRecordDto.class));
        return Result.success(pageResult);
    }

    @Deprecated
    @ApiOperation("申购记录查询")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "investSerial",value = "申购编号")
    )
    @Override
    public Result<SubscribeRecordDto> subscribeInfo(String investSerial) {
        return null;
    }


//    @ApiOperation("黄金申购")

}
